package com.gnerv.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.cms.entity.Article;
import com.gnerv.cms.entity.Content;
import com.gnerv.cms.mapper.ArticleMapper;
import com.gnerv.cms.service.IArticleService;
import com.gnerv.cms.service.ICatalogService;
import com.gnerv.cms.service.IContentService;
import com.gnerv.cms.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * CMS平台-文章表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-07-01
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    IContentService contentService;

    @Autowired
    ICatalogService catalogService;

    @Override
    public boolean createArticle(String cId, Article article, Content content) {
        String aId = UUIDUtil.getLower();
        article.setbId(aId);
        article.setReleaseTime(LocalDateTime.now());
        articleMapper.insert(article);
        content.setaId(aId);
        contentService.createContent(content);
        catalogService.bindingArticle(cId, aId);
        return true;
    }

    @Override
    public boolean deleteArticle(String bId) {
        articleMapper.delete(new QueryWrapper<>(new Article(bId)));
        contentService.deleteContentByAid(bId);
        catalogService.relieveArticle(bId);
        return true;
    }

    @Override
    public boolean updateArticle(String cId, Article article, Content content) {
        articleMapper.update(article, new QueryWrapper<>(new Article(article.getbId())));
        contentService.updateContent(content);
        catalogService.bindingArticle(cId, article.getbId());
        return true;
    }

    @Override
    public Article selectArticle(String bId) {
        Article article = articleMapper.selectOne(new QueryWrapper<>(new Article(bId)));
        return article;
    }

    @Override
    public List<Article> listArticle(Article article) {
        List<Article> articles = articleMapper.selectList(new QueryWrapper<>(article));
        return articles;
    }

    @Override
    public List<Article> selectArticleByCid(String cId) {
        List<Article> articles = articleMapper.selectArticleByCid(cId);
        return articles;
    }
}
